package com.ruoyi.module.productCatalog.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.productCatalog.vo.ProductCatalogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.productCatalog.mapper.ProductCatalogMapper;
import com.ruoyi.module.productCatalog.domain.ProductCatalog;
import com.ruoyi.module.productCatalog.service.IProductCatalogService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 页签2.0Service业务层处理
 * 
 * @author yqq
 * @date 2024-04-15
 */
@Service
public class ProductCatalogServiceImpl implements IProductCatalogService 
{
    @Autowired
    private ProductCatalogMapper productCatalogMapper;

    /**
     * 查询页签2.0
     * 
     * @param catalogId 页签2.0主键
     * @return 页签2.0
     */
    @Override
    public ProductCatalog selectProductCatalogByCatalogId(Long catalogId)
    {
        return productCatalogMapper.selectProductCatalogByCatalogId(catalogId);
    }

    /**
     * 查询页签2.0列表
     * 
     * @param productCatalog 页签2.0
     * @return 页签2.0
     */
    @Override
    public List<ProductCatalog> selectProductCatalogList(ProductCatalog productCatalog)
    {
        return productCatalogMapper.selectProductCatalogList(productCatalog);
    }

    /**
     * 新增页签2.0
     * 
     * @param productCatalog 页签2.0
     * @return 结果
     */
    @Override
    public int insertProductCatalog(ProductCatalog productCatalog)
    {
        productCatalog.setCreateTime(DateUtils.getNowDate());
        return productCatalogMapper.insertProductCatalog(productCatalog);
    }

    /**
     * 修改页签2.0
     * 
     * @param productCatalog 页签2.0
     * @return 结果
     */
    @Override
    public int updateProductCatalog(ProductCatalog productCatalog)
    {
        productCatalog.setUpdateTime(DateUtils.getNowDate());
        return productCatalogMapper.updateProductCatalog(productCatalog);
    }

    /**
     * 批量删除页签2.0
     * 
     * @param catalogIds 需要删除的页签2.0主键
     * @return 结果
     */
    @Override
    public int deleteProductCatalogByCatalogIds(Long[] catalogIds)
    {
        return productCatalogMapper.deleteProductCatalogByCatalogIds(catalogIds);
    }

    /**
     * 删除页签2.0信息
     * 
     * @param catalogId 页签2.0主键
     * @return 结果
     */
    @Override
    public int deleteProductCatalogByCatalogId(Long catalogId)
    {
        return productCatalogMapper.deleteProductCatalogByCatalogId(catalogId);
    }

    /**
     * 查询页签2.0列表
     * @param productCatalog
     * @return
     */
    @Override
    public List<ProductCatalogVO> selectAllProductCatalogList(ProductCatalog productCatalog) {
        List<ProductCatalogVO> list = new ArrayList<>();
        startPage();
        productCatalog.setParentId(0L);
        List<ProductCatalog> productCatalogList = selectProductCatalogList(productCatalog);
        if (CollUtil.isNotEmpty(productCatalogList)){
            for (ProductCatalog catalog : productCatalogList) {
                ProductCatalogVO productCatalogVO = new ProductCatalogVO();
                BeanUtils.copyProperties(catalog,productCatalogVO);

                ProductCatalog vo = new ProductCatalog();
                vo.setParentId(catalog.getCatalogId());
                List<ProductCatalog> catalogs = selectProductCatalogList(vo);
                productCatalogVO.setChildren(catalogs);
                list.add(productCatalogVO);
            }
        }
        return list;
    }

    /**
     * 删除一级及一级以下类型
     * @param catalogId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllProductCatalog(Long catalogId) {
        ProductCatalog productCatalog = productCatalogMapper.selectProductCatalogByCatalogId(catalogId);
        if (Objects.isNull(productCatalog)){
            throw new BaseException("产品目录不存在");
        }
        if (productCatalog.getParentId()==0L){
            productCatalogMapper.deleteProductCatalogByCatalogId(catalogId);
            productCatalogMapper.deleteProductCatalogByParentId(catalogId);
        }else{
            productCatalogMapper.deleteProductCatalogByCatalogId(catalogId);
        }
    }
}
